package com.fairytask.ui
{
	import com.fairytask.data.RecordObject;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.RecordDefine;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.ResManager;
	import com.fairytask.ui.base.FrameUI;
	import com.greensock.TweenNano;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.filters.GlowFilter;

	public class WorldMapUI extends FrameUI
	{
		private static var _self	:WorldMapUI=null;
		
		private var mapMc		:MovieClip;
		private var mapSp		:Sprite;
//		private var closeMc	:Button	=	new Button("",47,47,"Button_0113");
//		private var mapBg		:Clip	=	new Clip();
		private var loadShape	:Shape;		//加载背景
		private var loadRes	:MovieClip;	//加载进度条
		//数据
		private var _dataArr	:Array	=	[];
		private var _mask		:Sprite	=	new Sprite();			//蒙版的区域
		//主城id，用来默认点亮
//		private const CITY		:int = 10000000;
		
		public static function getInstance():WorldMapUI
		{
			if(_self==null)
			{
				_self=new WorldMapUI();
			}
			return _self;
		}
		
		public function WorldMapUI(popUI:Boolean=false)
		{
			super(popUI);
			init()
			initBind();
		}
		
		/**
		*初始化 
		* 
		*/		
		private function init():void
		{
			windowBG.addChild(_mask);
			//QueueLoader.StartLoad(ResConfig.swfResPath+"world.swf",onWorld,onProcess);
			
			mapSp = new Sprite();
			
			windowBG.addChild(mapSp);
			//关闭位置
			/*closeMc.x	=	935;
			closeMc.y	=	9;
			windowBG.addChild(closeMc);*/
			
			loadShape = new Shape();
			loadShape.graphics.beginFill(0x0066CC,0.2);
			loadShape.graphics.drawRect(-GameStage.x,-GameStage.y,GameStage.stageWidth,GameStage.stageWidth);
			loadShape.graphics.endFill();
			//windowBG.addChild(loadShape);
			
			loadRes = ResManager.newResClass("loadingBar") as MovieClip;
			loadRes.barText.selectable = false;
			loadRes.barText.visible = false;
			loadRes.masker.width = 0;
			loadRes.x = 345;
			loadRes.y = 240;
			windowBG.addChild(loadRes);
			_mask.graphics.beginFill(0,1);
			_mask.graphics.drawRect(-GameStage.x,-GameStage.y,GameStage.stageWidth,GameStage.stageWidth);
			_mask.graphics.endFill();
			//默认主城点亮
//			_dataArr["world_"+CITY]	=	true;
			//关闭事件
//			closeMc.addEventListener(MouseEvent.CLICK,onClose);
		}
		
		override protected function stageReSize(evt:Event=null):void
		{
			_mask.graphics.clear();
			_mask.graphics.beginFill(0,1);
			_mask.graphics.drawRect(-GameStage.x,-GameStage.y,GameStage.stageWidth,GameStage.stageWidth);
			_mask.graphics.endFill();
		}

		
		/**
		 *初始化数据 
		 * 
		 */		
		private function initBind():void
		{
			bindManager.BindRecord(model.scene,RecordDefine.SCENEOPEN,onSceneAdd,null);
		}
		
		/**
		 * 添加可进入场景 
		 * @param key
		 * @param val
		 * 
		 */
		private function onSceneAdd(key:int,val:RecordObject):void
		{
			var tmpSceneId	:int	=	val.GetProperties("0");
			if(_dataArr["world_"+tmpSceneId]!=null)
			{
				_dataArr["world_"+tmpSceneId].isGray	=	false;
				_dataArr["world_"+tmpSceneId].filters	=	null;
			}
			else
			{
				_dataArr["world_"+tmpSceneId]	=	true;
			}
		}
		
		private function onWorld(val:MovieClip,pList:Array=null):void
		{
			if(val!=null)
			{
				mapMc = val;
				onWorldShow();
			}
			else
			{
				//QueueLoader.StartLoad(ResConfig.swfResPath+"world.swf",onWorld);
			}
		}
		
		private function onProcess(val:ProgressEvent):void
		{
			//到达宽度
			var maskToWidth:int = int(val.bytesLoaded / val.bytesTotal * loadRes.width);
			//滚动时间
			var time:Number = (int(val.bytesLoaded / val.bytesTotal * loadRes.width) - loadRes.masker.width)/loadRes.width * 1;
			if(maskToWidth == loadRes.width)
			{
				TweenNano.to(loadRes.masker,time,{width:maskToWidth,onComplete:onWorldShow});
			}else
			{
				TweenNano.to(loadRes.masker,time,{width:maskToWidth});
			}
		}
		
		private function onWorldShow():void
		{
			if(mapMc)
			{
				if(mapSp.contains(mapMc))
				{
					return;
				}
				mapSp.addChild(mapMc);
				if(loadShape.parent)
				{
					loadShape.parent.removeChild(loadShape);
				}
				if(loadRes.parent)
				{
					loadRes.parent.removeChild(loadRes);
				}
				for(var i:int=0;i<mapMc.numChildren;i++)
				{
					if(mapMc.getChildAt(i) is MovieClip)
					{
						var tmpMc:MovieClip	=mapMc.getChildAt(i) as MovieClip;
						if(_dataArr[tmpMc.name])
						{
							tmpMc.isGray	=	false;
						}
						else
						{
							tmpMc.isGray	=	true;
							tmpMc.filters	=	[Filter.GRAY];
						}
						_dataArr[tmpMc.name]	=	tmpMc;
						
						tmpMc.addEventListener(MouseEvent.ROLL_OVER, onItemOver);
						tmpMc.addEventListener(MouseEvent.ROLL_OUT, onItemOut);
						tmpMc.addEventListener(MouseEvent.CLICK, onItemClick);
					}
				}
			}
			
		}
		
		private function onItemOver(evt:MouseEvent):void
		{
			var worldname:String=evt.target.name;
			var arr:Array=worldname.split("_");
			evt.currentTarget.filters	=	[new GlowFilter(0xff0000,1,5,5,3)];
		}
		
		private function onItemOut(evt:MouseEvent):void
		{
			evt.currentTarget.filters=null;
			if(evt.currentTarget.isGray)
			{
				evt.currentTarget.filters	=	[Filter.GRAY];
			}
		}
		
		private function onItemClick(evt:MouseEvent):void
		{
			var worldname:String=evt.target.name;
			var arr:Array=worldname.split("_");
			if(int(arr[1])!=model.scene.ConfigID)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_WORLD_TRANSFER,int(arr[1]));
			}
			else
			{
				hide();
			}
		}
		
		private function onClose(e:MouseEvent):void
		{
			hide();
		}
		
		/**
		 * 重写显示 
		 * @param space
		 * @param onshow
		 * @param argc
		 * 
		 */
		override public function show(space:String=TOPLAYER, onshow:Function=null):void
		{
			autoPos	=	false;
//			autoSendMsg	= false;
			super.show(space,onshow);
		}
	}
}